//
//  MSGLogInView.m
//  QuickCure1
//
//  Created by apple on 2016/11/1.
//  Copyright © 2016年 maoge. All rights reserved.
//

#import "MSGLogInView.h"
#import <SMS_SDK/SMSSDK.h>

@interface MSGLogInView ()

@property (weak, nonatomic) IBOutlet UITextField *textFilePhoneNum;

@property (weak, nonatomic) IBOutlet UITextField *textFileCheckNum;

@property (weak, nonatomic) IBOutlet UIButton *buttonSendMsg;

@property (nonatomic,assign)int timeNum;

@property (nonatomic,strong)NSTimer *time;

@end

@implementation MSGLogInView


+ (instancetype)getMSGView{
    
    return [[[NSBundle mainBundle]loadNibNamed:@"SendMSGLogInView" owner:nil options:nil]lastObject];
    
}
//发送验证码
- (IBAction)clickSendMSG:(UIButton *)sender {
    self.timeNum = 60;
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.textFilePhoneNum.text zone:@"86" customIdentifier:nil result:^(NSError *error) {
        if (error == nil) {
            NSLog(@"发送成功");
            sender.enabled = NO;
            NSTimer *time = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(setGetMsgButtonTitle) userInfo:nil repeats:YES];
            [[NSRunLoop currentRunLoop] addTimer:time forMode:NSRunLoopCommonModes];
            self.time = time;
        }else{
            NSLog(@"发送失败%@",error);
        }
    }];
}

//定时器方法
- (void)setGetMsgButtonTitle{
    if (self.timeNum < 0) {
        [self.time invalidate];
        self.buttonSendMsg.enabled = YES;
        return;
    }
    [self.buttonSendMsg setTitle:[NSString stringWithFormat:@"%d秒",self.timeNum] forState:UIControlStateDisabled];
    [self.buttonSendMsg setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
    self.timeNum = self.timeNum - 1;
}

//登录按钮
- (IBAction)clickButtonLogIn:(UIButton *)sender {
    
    [SMSSDK commitVerificationCode:self.textFileCheckNum.text phoneNumber:self.textFilePhoneNum.text zone:@"86" result:^(SMSSDKUserInfo *userInfo, NSError *error) {
        if (error == nil) {
            NSLog(@"登录成功");
            NSString *userName = self.textFilePhoneNum.text;
            NSString *passWord = self.textFileCheckNum.text;
            
            [[NetWorkManager sharedManager] loginUserWihtAccount:userName password:passWord successBlock:^(Users *dto) {
                if (self.CompleteBlock) {
                    _CompleteBlock(dto);
                }
            } errorBlock:^(NSString *error) {
                NSLog(@"账号密码错误");
            }];
            
        }else{
            NSLog(@"登录失败");
        }
    }];
}

//通过注册密码登录--> tag = 1
- (IBAction)clickButtonRegister:(UIButton *)sender {
    
    self.index = sender.tag;
    [self sendActionsForControlEvents:UIControlEventValueChanged];
}

//注册新的账号--> tag = 2
- (IBAction)clickRegisterNewID:(UIButton *)sender {
    self.index = sender.tag;
    [self sendActionsForControlEvents:UIControlEventValueChanged];
}

//忘记密码--> tag = 3
- (IBAction)clickButtonForgetPassWord:(UIButton *)sender {
    self.index = sender.tag;
    [self sendActionsForControlEvents:UIControlEventValueChanged];
}

#pragma mark
#pragma mark - 点击屏幕取消编辑模式
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.textFileCheckNum endEditing:YES];
    [self.textFilePhoneNum endEditing:YES];
    
}


@end
